package com.chinh.service.impl;

import com.chinh.annotation.AopTest;
import com.chinh.entity.SysUser;
import com.chinh.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author chinh
 * @date 2020/9/21
 */
@Service
public class TestServiceImpl implements TestService {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	@AopTest
	public void sayHello() {
		System.out.println("hello");
	}

	@Override
	public SysUser selectById(Long id) {
		String sql = "select * from sys_user where id = ?";
		RowMapper<SysUser> userRowMapper = new BeanPropertyRowMapper<SysUser>(SysUser.class);
		return jdbcTemplate.queryForObject(sql, userRowMapper, id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int updateUserName(SysUser sysUser) {
		String sql = "update sys_user set username=? where id=?";
		Object[] obj = new Object[]{sysUser.getUsername(), sysUser.getId()};
		int num = this.jdbcTemplate.update(sql, obj);
		System.out.println(num);
		if (1 == num) throw new RuntimeException();
		return num;
	}
}
